﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Linq;

using Refix.Core.Options;
using Refix.Core.Properties;
using Refix.Core.Repository;

namespace Refix.Core.Commands
{
    public class Alternative : Command
    {
        protected override string HelpText
        {
            get { return ResourceDispenser.GetString("ALTERNATIVE_HELP"); }
        }

        public override bool Validate()
        {
            if (!base.Validate())
            {
                return false;
            }
            if (Options.Arguments.Count != 3)
            {
                OutputWriter.WriteLine(OutputVerbosity.Quiet, ResourceDispenser.GetString("WRONG_NUMBER_OF_ARGUMENTS"));
                ShowHelp();
                return false;
            }
            return true;
        }

        public override bool Execute()
        {
            LocalRepository localRepository = GetLocalRepository();
            string assemblyName = Options.Arguments[0];
            string version = Options.Arguments[1];
            string alternativeVersion = Options.Arguments[2];
            Component component = localRepository.GetComponent(assemblyName);
            if (component == null)
            {
                OutputWriter.WriteLine(OutputVerbosity.Quiet, ResourceDispenser.GetString("ALTERNATIVE_UNKNOWN_ASSEMBLY"),
                                   assemblyName);
                return false;
            }
            // We don't use GetComponentVersion, because we want to persist the component later.
            ComponentVersion componentVersion = component.Versions.SingleOrDefault(v => v.Number == version);
            if (componentVersion == null)
            {
                OutputWriter.WriteLine(OutputVerbosity.Quiet, ResourceDispenser.GetString("ALTERNATIVE_UNKNOWN_VERSION"),
                                   version, assemblyName);
                return false;
            }
            if (Options.Options.Any(o => o is Delete))
            {
                if (!componentVersion.AlternativeVersions.Any(a => a.Number == alternativeVersion))
                {
                    OutputWriter.WriteLine(ResourceDispenser.GetString("ALTERNATIVE_NOT_SET"), alternativeVersion, version,
                                       assemblyName);
                    return false;
                }
                componentVersion.AlternativeVersions.RemoveAll(v => v.Number == alternativeVersion);
                OutputWriter.WriteLine(ResourceDispenser.GetString("ALTERNATIVE_DELETED"), alternativeVersion, version,
                                   assemblyName);
            }
            else
            {
                if (componentVersion.AlternativeVersions.Any(a => a.Number == alternativeVersion))
                {
                    OutputWriter.WriteLine(ResourceDispenser.GetString("ALTERNATIVE_ALREADY_SET"), alternativeVersion,
                                       version, assemblyName);
                    return true;
                }
                componentVersion.AlternativeVersions.Add(new AlternativeVersion {Number = alternativeVersion});
                OutputWriter.WriteLine(ResourceDispenser.GetString("ALTERNATIVE_SET"), alternativeVersion, version,
                                   assemblyName);
            }
            localRepository.PutComponent(component);
            return true;
        }
    }
}